﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;

public class BatchModifyLabel : Editor
{

    [MenuItem("MyTool/ModifyLabelFont")]

    static void OneKeyModifyLabelFont()
    {
        string[] fullPaths = GetSelectPaths();

        if (fullPaths.Length == 0)
        {
            if (EditorUtility.DisplayDialog("提示", "请至少选择一个目录或者文件", "ok"))
            {
                return;
            }
            return;
        }

        string messageStr = string.Empty;
        for (int i = 0; i < fullPaths.Length; i++)
        {
            messageStr += fullPaths[i] + "\r\n";
        }

        if (EditorUtility.DisplayDialog("批量修改label的字体", string.Format("你要修改预制体的来源为{0}", messageStr), "confirm", "cancel"))
        {
            Font font = GetTargetFont();
            string[] files = GetAllFilesString(fullPaths);
            for (int i = 0; i < files.Length; i++)
            {
                EditorUtility.DisplayProgressBar("修改字体", "批量修改字体中。。。", 1f * i / files.Length);
                ModifyLabelFont(files[i], font);
            }
        }
        AssetDatabase.Refresh();
        EditorUtility.ClearProgressBar();
    }

    static string[] GetAllFilesString(string[] paths)
    {
        List<string> ret = new List<string>();
        for (int i = 0; i < paths.Length; i++)
        {
            if (Directory.Exists(paths[i]))
            {
                DirectoryInfo dir = new DirectoryInfo(paths[i]);
                var files = dir.GetFiles("*", SearchOption.AllDirectories);
                for (int j = 0; j < files.Length; j++)
                {
                    if (files[j].FullName.EndsWith(".meta"))
                    {
                        continue;
                    }
                    ret.Add(files[j].FullName);
                }               
            }else if (File.Exists(paths[i]))
            {
                var file = new FileInfo(paths[i]);
                if (file.FullName.EndsWith(".meta"))
                {
                    continue;
                }
                ret.Add(file.FullName);
            }
        }

        for (int i = 0; i < ret.Count; i++)
        {
            ret[i] = ret[i].Replace("\\", "/");
            ret[i] = ret[i].Replace(Application.dataPath + "/Resources/", "");
        }

        return ret.ToArray();
    }

    static Font GetTargetFont()
    {
        Font font = Resources.Load("fonts/Font_SC") as Font;
        if(font == null)
        {
            Debug.LogError("font is null");
        }
        return font;
    }

    static void ModifyLabelFont(string path,Font font)
    {
        //path = path.Substring(path.IndexOf("Resources"));
        path = path.Split('.')[0];
        GameObject temp = Resources.Load(path) as GameObject;
        //GameObject obj = Instantiate(temp);

        //UILabel[] labels = temp.transform.GetComponentsInChildren<UILabel>();
        List<UILabel> labels = new List<UILabel>();
        if(temp == null)
        {
            Debug.Log(path);
            return;
        }
        GetComponentsInChilds<UILabel>(temp.transform, ref labels);

        for (int i = 0; i < labels.Count; i++)
        {
            labels[i].trueTypeFont = font;
            labels[i].bitmapFont = null;
        }

        EditorUtility.SetDirty(temp);
        //var targetPrefab = UnityEditor.PrefabUtility.GetPrefabParent(temp) as GameObject;
        //if (targetPrefab != null)
        //{
        //    //UnityEditor.Prefab
        //    UnityEditor.PrefabUtility.ReplacePrefab(temp, targetPrefab);
        //}
    }

    static void GetComponentsInChilds<T>(Transform tran, ref List<T> lst)
    {
        if(tran == null)
        {
            return ;
        }
        for (int i = 0; i < tran.childCount; i++)
        {
            T com = tran.GetChild(i).GetComponent<T>();
            if (com != null)
            {
                lst.Add(com);
            }
            GetComponentsInChilds<T>(tran.GetChild(i),ref lst);

        }
    }

    static string[] GetSelectPaths()
    {
        UnityEngine.Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.Unfiltered);
        //此处添加需要命名的资源后缀名,注意大小写。
        string[] paths = new string[SelectedAsset.Length];

        for (int i = 0; i < paths.Length; i++)
        {
            paths[i] = AssetDatabase.GetAssetPath(SelectedAsset[i]);
        }

        return paths;
    }
}
